<!--
 * @Author: your name
 * @Date: 2020-03-02 10:49:55
 * @LastEditTime: 2020-06-11 10:46:39
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \MPShop\mixins\login.vue
--> 
<script>
import { customerLogin } from "@/lib/api/customerServer/info";
import walletMixin from "@/mixins/wallet";
import { mapMutations } from "vuex";
export default {
	mixins:[walletMixin],
	methods: {
		...mapMutations("login", ["saveLoginInfo"]),

		async login(e) {
			const userInfo = e.detail.userInfo;
			if (!userInfo) {
				throw new Error({
					resultInfo: "登录失败"
				});
			}
			const code = await this.getCode();
			await this.codeLogin(code, userInfo);
		},

		getCode: function() {
			return new Promise((resolve, reject) => {
				uni.login({
					success: res => {
						console.log("res", res);
						resolve(res.code);
					},
					fail: error => {
						console.log("error", error);
						reject(error);
					}
				});
			});
		},

		/**
		 * @param { string } code // platform code
		 * @param { Object } userInfo // platform userInfo
		 */
		codeLogin(code, userInfo) {
			const propsData = {
				showLoading: false,
				jsCode: code,
				headImage: userInfo.avatarUrl,
				city: userInfo.city,
				gender: userInfo.gender,
				nickname: userInfo.nickName,
				province: userInfo.province,
				type: this.$api.platform().code
			};
			return customerLogin(propsData).then(res => {
				//console.log(res);
				this.saveLoginInfo(res.data);
				return this._getWallet();
			})
		}
	}
};
</script>